package cn.com.dashihui.web.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.upload.UploadFile;

import cn.com.dashihui.kit.DirKit;
import cn.com.dashihui.web.base.BaseController;
import cn.com.dashihui.web.dao.Baby;
import cn.com.dashihui.web.dao.BabyTitle;
import cn.com.dashihui.web.service.BabyService;

public class BabyController extends BaseController{
	private BabyService service = new BabyService();
    
    public void index(){
    	List<Baby> list=service.findAll();
    	setAttr("adList", list);
    	setAttr("title", service.findBabyTitle());
    	setAttr("size", list.size());
        render("index.jsp");
    }
    
    public void doSort(){
    	Map<String,String[]> params = getParaMap();
    	if(params!=null&&params.size()!=0&&params.containsKey("sortKey")){
    		String sortKey = params.get("sortKey")[0];
    		Map<String,String> sortMap = new HashMap<String,String>();
    		for(String key : params.keySet()){
    			if(!key.equals("sortKey")){
    				String id = key.replace(sortKey, "");
    				String no = params.get(key)[0];
    				if(StrKit.isBlank(no)||no.length()>3){
    					no = "0";
    				}
    				sortMap.put(id, no);
    			}
    		}
    		service.sortAd(sortMap);
    		renderSuccess();
    		return;
    	}
    	renderFailed();
    }
    
	public void toAdd(){
		render("add.jsp");
	}
	public void goodsPage(){
		int sCategoryonid = getParaToInt("sCategoryonid",0);
		int sCategorytwid = getParaToInt("sCategorytwid",0);
		int sGoodId = getParaToInt("sGoodId",0);
		String sSellerId = getPara("sSellerId");
		String sStoreId = getPara("sStoreId");
		String keyword = getPara("k");
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		renderSuccess(service.getGoodsEge(sGoodId,sCategoryonid, sCategorytwid,  keyword, pageNum, pageSize));
	}
	/**
	 * 添加
	 * @return -1：异常，0：成功，1：没有关联商品，2：图片为空，3：图片上传失败
	 */
	public void doAdd(){
		UploadFile cThumb = getFile("cThumb");
		int type = getParaToInt("type",0);
		String thumb=getPara("goodsthumb");
		String goodsid = getPara("goodsid");
		int isShow = getParaToInt("isShow",0);
		if(type==2&&StrKit.isBlank(goodsid)){
			renderResult(1);
			return;
		}else if(type==2&&!StrKit.isBlank(goodsid)){
			//保存
			Baby ad = new Baby()
				.set("type", type)
				.set("link", "http://shop.bain.net.cn/goods/jfdetail/"+goodsid)
				.set("goodsid", goodsid)
				.set("isShow", isShow);
			// 如果上传了图片，则上传至FTP，并记录图片文件名
			if (cThumb != null) {
				String thumbFileName;
				String dir = DirKit.getDir(DirKit.AD);
				try {
					thumbFileName = uploadToFtp(dir, cThumb);
				} catch (IOException e) {
					e.printStackTrace();
					renderResult(3);
					return;
				}
				ad.set("thumb", dir.concat(thumbFileName));
			}
				if(service.addAd(ad)){
					renderSuccess(service.findById(ad.getInt("id")));
					return;
				}
		}else{
			renderResult(1);
			return;
		}
	}
	
	public void toSetGoods(){
		render("setGoods.jsp");
	}
	
	public void toEdit(){
		int id = getParaToInt(0,0);
		if(id!=0){
			setAttr("object", service.findById(id));
		}
		render("edit.jsp");
	}
	/**
	 * 编辑人气宝贝名称
	 */
	public void doEdits(){
		int id = getParaToInt(0,0);
		if(id!=0){
			setAttr("object", service.findBabyTitleById(id));
		}
		render("editTitle.jsp");
	}
	public void dosEdit(){
		int id = getParaToInt("ids",0);
		String name=getPara("names");
		if(id!=0){
			BabyTitle by= service.findBabyTitleById(id);
			by.set("title", name);
			if(by.update()){
				renderSuccess();
				return;
			}
		}
	}
	/**
	 * 更新
	 * @return -1：异常，0：成功，1：没有关联商品，2：图片上传失败
	 */
	public void doEdit(){
		int adid = getParaToInt("adid",0);
		int type = getParaToInt("type");
		String thumb=getPara("goodsthumb");
		String goodsid = getPara("goodsid");
		int isShow = getParaToInt("isShow",0);
		if(type==2&&StrKit.isBlank(goodsid)){
			renderResult(1);
			return;
		}else if(type==2&&!StrKit.isBlank(goodsid)){
			//保存
			Baby ad = service.findById(adid);
				ad.set("type", type)
				.set("thumb", thumb)
				.set("link", "http://shop.bain.net.cn/goods/jfdetail/"+goodsid)
				.set("goodsid", goodsid)
				.set("isShow", isShow);
				if(service.editAd(ad)){
					renderSuccess(service.findById(ad.getInt("id")));
					return;
				}
		}else{
			renderResult(1);
			return;
		}
	}
	
	/**
	 * 删除
	 * @return -1：删除失败，0：删除成功
	 */
	public void doDelete(){
		int id = getParaToInt(0,0);
		if(id!=0){
			Baby ad = service.findById(id);
			//先判断社区如果上传了图片，则删除图片
			if(!StrKit.isBlank(ad.getStr("thumb"))){
				try {
					deleteFromFtp(ad.getStr("thumb"));
				} catch (IOException e) {
					logger.error("删除时，相关图片"+ad.getStr("thumb")+"删除失败！");
					e.printStackTrace();
				}
			}
			//然后再对记录进行删除
			ad.delete();
//			logger.info("删除广告位【"+id+"】，及其图片");
			renderSuccess();
			return;
		}
		renderFailed();
	}
}
